package com.revai.engine.basic.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtil {

  /**
   * 导出excel到输出流
   * 
   * @param list
   * @param out
   * @throws IOException
   */
  public static void exportToOutputStream(List<?> list, OutputStream out) throws IOException {
    if (list == null) {
      return;
    }

    // 1. 创建工作簿
    XSSFWorkbook workbook = new XSSFWorkbook();
    // 2. 创建工作表
    XSSFSheet sheet = workbook.createSheet("Sheet1");
    // 3. 写入数据
    for (int i = 0; i < list.size(); i++) {
      Object obj = list.get(i);
    }

    workbook.write(out);
    workbook.close();
  }

  public static void main(String[] args) {
    // 1. 创建工作簿
    XSSFWorkbook workbook = new XSSFWorkbook();
    // 2. 创建工作表
    XSSFSheet sheet = workbook.createSheet("WriterDataTest");
    // 3. 模拟待写入数据
    Map<String, Object[]> data = new TreeMap<>();
    data.put("1", new Object[] { "ID", "NAME", "LASTNAME" });
    data.put("2", new Object[] { 1, "Amit", "Shukla" });
    data.put("3", new Object[] { 2, "Lokesh", "Gupta" });
    data.put("4", new Object[] { 3, "John", "Adwards" });
    data.put("5", new Object[] { 4, "Brian", "Schultz" });
    // 4. 遍历数据写入表中
    Set<String> keySet = data.keySet();
    int rowNum = 0;
    for (String key : keySet) {
      XSSFRow row = sheet.createRow(rowNum++);
      Object[] objArr = data.get(key);
      int cellNum = 0;
      for (Object obj : objArr) {
        XSSFCell cell = row.createCell(cellNum++);
        if (obj instanceof String) {
          cell.setCellValue((String) obj);
        } else if (obj instanceof Integer) {
          cell.setCellValue((Integer) obj);
        }
      }
    }
    try {
      File file = new File("Test.xlsx");
      FileOutputStream out = new FileOutputStream(file);
      workbook.write(out);
      workbook.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
